﻿// <copyright file="GameProviderExtension.cs" company="Ecole Normale Supérieure">
// Released under GPLv2
// </copyright>
// <summary>Contains the class GameProviderExtension</summary>

namespace Sqwarea.GameLogic.Extensions
{
    using System.Drawing;
    using Sqwarea.Persistence;

    /// <summary>
    /// Contains useful functions between Persistence.GameProvider and GameLogic providers
    /// </summary>
    public static class GameProviderExtension
    {
        /// <summary>
        /// Return true if and only if the king occupied a square near the point position.
        /// Used by AttackSolver (to know if a square can be given to a king or not)
        /// </summary>
        /// <param name="gameProvider">The gameProvider to extend</param>
        /// <param name="position">The positon of the considered square</param>
        /// <param name="kingId">The considered king</param>
        /// <returns>See summary.</returns>
        public static bool IsSquareNearKingdom(this IGameProvider gameProvider, Point position, long kingId)
        {
            var rectangle = new Rectangle(position.X - 1, position.Y - 1, 3, 3);
            var squares = gameProvider.GetSquaresInRectangle(rectangle);
            return squares[0, 1].KingId == kingId ||
                squares[1, 0].KingId == kingId ||
                squares[1, 1].KingId == kingId ||
                squares[1, 2].KingId == kingId ||
                squares[2, 1].KingId == kingId;
        }
    }
}
